package com.omo.domain.machine.excel;

import com.omo.domain.machine.model.entity.MachineProductionEntity;
import com.omo.types.excel.BaseExportExcelData;
import com.omo.types.excel.BaseExportExcelDataBody;
import com.omo.types.excel.BaseExportExcelInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class MachineProductionExportExcelInfo extends BaseExportExcelInfo<MachineProductionExportExcelInfo.ExportExcelMachineProductionEntity> {

    @Value("${excel.export-template-path.MachineProduction}")
    private String templatePath;

    private Boolean expand = false;

    private static final Integer expandParam = 3;

    private static final Integer D_ASCii = 68;

    private static final Integer D_INDEX = 3;

    public BaseExportExcelInfo.BaseExportExcelEntity<ExportExcelMachineProductionEntity> buildExportExcelEntity(
            ExportExcelMachineProductionEntity data, Collection expandCollection,String machineName) {
        return BaseExportExcelInfo.BaseExportExcelEntity.<ExportExcelMachineProductionEntity>builder()
                .suffix(machineName)
                .templatePath(templatePath)
                .data(data)
                .expand(expand)
                .expandParam(expandParam)
                .expandCollection(expandCollection)
                .expandBeginIndex(D_INDEX)
                .expandBeginIndexASCii(D_ASCii)
                .build();
    }

    @Override
    public Boolean setExpand(Boolean expand) {
        return this.expand = expand;
    }

    @Data
    public static class ExportExcelMachineProductionEntity extends BaseExportExcelData<BaseExportExcelDataBody> {
        private EEMachineProductionTitle title;
    }

    public EEMachineProductionTitle builderTitle(Set<String> expandCollections) {
        EEMachineProductionTitle title = new EEMachineProductionTitle();
        Class<EEMachineProductionTitle> titleClass = EEMachineProductionTitle.class;
        int index = 0;
        try {
            for (String expandCollection : expandCollections){
                // 动态设置标题列
                Method method = titleClass.getMethod("setVersionTitle" + (char) (D_ASCii + index), String.class);
                method.invoke(title,expandCollection);
                index += expandParam;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return title;
    }

    public List<BaseExportExcelDataBody> builderBody(List<MachineProductionEntity> entities) {
        Map<String,EEMachineProductionBody> mergedMap = new HashMap<>();

        int index = D_ASCii;
        try {
            for (MachineProductionEntity entity : entities){
                // 构建分组key
                String versionKey = entity.getMachineSn();
                EEMachineProductionBody target = mergedMap.get(versionKey);
                if (target == null){
                    target = EEMachineProductionBody.builder()
                            .machineSn(entity.getMachineSn())
                            .machineName(entity.getMachineName())
                            .testTime(entity.getTestTime().toString())
                            .updateTime(entity.getUpdateTime().toString())
                            .build();
                    mergedMap.put(versionKey, target);
                }
                Class<EEMachineProductionBody> bodyClass = EEMachineProductionBody.class;
                Method preProductionMethod = bodyClass.getMethod("setValue" + (char) (index), String.class);
                preProductionMethod.invoke(target,entity.getPreProduction().toString());
                index ++;
                Method productionMethod = bodyClass.getMethod("setValue" + (char) (index), String.class);
                productionMethod.invoke(target,entity.getProduction().toString());
                index ++;
                Method differenceMethod = bodyClass.getMethod("setValue" + (char) (index), String.class);
                differenceMethod.invoke(target,entity.getDifference().toString());
                index ++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return new ArrayList<>(mergedMap.values());
    }

}
